package com.fuershiye.woman.monitor.controller;

import com.fuershiye.woman.monitor.beans.Page;
import com.fuershiye.woman.monitor.beans.Result;
import com.fuershiye.woman.monitor.beans.ViewDepartIndicatorReport;
import com.fuershiye.woman.monitor.constants.Massage;
import com.fuershiye.woman.monitor.model.Department;
import com.fuershiye.woman.monitor.service.DepartIndicatorService;
import com.fuershiye.woman.monitor.service.DepartmentService;
import com.fuershiye.woman.monitor.utils.DateFormatUtil;
import com.fuershiye.woman.monitor.utils.IndicatorUtil;
import com.google.common.collect.Lists;
import org.apache.commons.lang3.StringUtils;
import org.slf4j.Logger;
import org.slf4j.LoggerFactory;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Controller;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.ResponseBody;

import java.util.List;


/**
 * 选择部分、部门A的指标，转移给部门B
 * 部门 A--> 部门B
 * @author admin
 */
@Controller
@RequestMapping(value = "/department/indicator")
public class DepartIndicatorController {

    private static final Logger logger = LoggerFactory
            .getLogger(DepartIndicatorController.class);

    @Autowired
    private DepartIndicatorService departIndicatorService;
    @Autowired
    private DepartmentService departmentService;

    /**
     *
     UPDATE `depart_indicator`
     SET `departid` =47, `mtime` = NOW()
     WHERE `departid` =47 and `indicator` =
                                (SELECT `id` FROM `indicator` WHERE `formsid` =1 and `code` ='A11');

     * @param indicatorIds  指标id【1,2,3,4,5,6,7】
     * @param formsid   【1 2】
     * @param fromDepartId  从谁
     * @param toDepartId    到谁
     * @return
     */
    // TODO 增加年份
    @RequestMapping(value = "/move.do")
    @ResponseBody
    public Result move(String indicatorIds, Long formsid, Long fromDepartId, Long toDepartId, Integer fromYear, Integer toYear) {

        if(StringUtils.isEmpty(indicatorIds)||formsid==null||formsid==0 || fromDepartId==null||fromDepartId==0
                || toDepartId==null||toDepartId==0
                || !DateFormatUtil.isYear(fromYear) || !DateFormatUtil.isYear(toYear)){
            logger.error("move error "
                    +",fromDepartId:"+fromDepartId
                    +",toDepartIds:"+toDepartId);
            return new Result(Massage.ERROR_PARAMETER);
        }

        logger.info("indicatorIds:"+indicatorIds+"formsid:"+formsid+"fromDepartId:"+fromDepartId+"toDepartId:"+toDepartId);
        Department fromDepartment = departmentService.get(fromDepartId);
        if(null==fromDepartment){
            return new Result(Massage.DEPART_IS_NULL);
        }
        Department toDepartment = departmentService.get(toDepartId);
        if(null==toDepartment){
            return new Result(Massage.DEPART_IS_NULL);
        }

        List<Long> indicators = IndicatorUtil.getIds(indicatorIds);

        List<Long> toDepartIds = Lists.newArrayList();
        toDepartIds.add(toDepartId);

        //存在一条就认为有问题，不能转移数据
        List<ViewDepartIndicatorReport> exitData = departIndicatorService.listByCondition(toDepartIds, formsid, indicators, toYear, null, new Page(1, Integer.MAX_VALUE));
        if(!exitData.isEmpty()){
            return new Result(Massage.DEPART_INDICATOR_IS_EXIT);
        }

        int result = 0;
        for (Long indicator : indicators) {
            result += departIndicatorService.updateDepart(fromDepartId, toDepartId, formsid, indicator, fromYear, toYear, 1);
        }

        if(indicators.size()!=result){
            logger.error("部分数据有丢失，请确认！！!");
        }
        return new Result(Massage.SUCCESS, result);
    }

}
